package com.topvision.face.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.kernal.demo.plateid.R;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/*定义一个画矩形框的类*/
public class SVDraw extends SurfaceView implements SurfaceHolder.Callback {

    protected SurfaceHolder sh;
    private int mWidth;
    private int mHeight;

    public SVDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
        sh = getHolder();
        sh.addCallback(this);
        sh.setFormat(PixelFormat.TRANSPARENT);
        setZOrderOnTop(true);
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int w, int h) {
        mWidth = w;
        mHeight = h;
    }

    public void surfaceCreated(SurfaceHolder arg0) {

    }

    public void surfaceDestroyed(SurfaceHolder arg0) {

    }

    public void clearDraw() {
        if (sh == null) {
            return;
        }
        Canvas canvas = sh.lockCanvas();
        if (canvas == null) {
            return;
        }
        canvas.drawColor(Color.TRANSPARENT);
        sh.unlockCanvasAndPost(canvas);
    }

    float eyesDistance; // 两眼之间的距离


    /**
     * 在画布上画图
     */
    public void drawImage(double[][] facesArray, boolean[] recognizeFlag) {
        if (facesArray == null || facesArray.length == 0) {
            return;
        }
        if (recognizeFlag == null) {
            recognizeFlag = new boolean[facesArray.length];
        }
        Canvas canvas = sh.lockCanvas();
        if (canvas != null) {
            // 以下四句消息图像重叠
            Paint paint = new Paint();
            paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
            canvas.drawPaint(paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC));

            int height = canvas.getHeight();
            int width = canvas.getWidth();
            Bitmap mImage = BitmapFactory.decodeResource(getResources(),
                    R.mipmap.person_face);
            Bitmap unknownImage = BitmapFactory.decodeResource(getResources(),
                    R.mipmap.person_face_red);
            int index = 0;
            for (double[] rect : facesArray) {
                float first = (float) (rect[0] * width);
                float second = (float) (rect[1] * height);
                float third = first + (float) (rect[2] * width);
                float fourth = second + (float) (rect[3] * height);
                if (recognizeFlag[index]) {
                    canvas.drawBitmap(unknownImage, null, new Rect((int) first,
                            (int) second, (int) third, (int) fourth), null);
                } else {
                    canvas.drawBitmap(mImage, null, new Rect((int) first,
                            (int) second, (int) third, (int) fourth), null);
                }
                index++;
            }
            sh.unlockCanvasAndPost(canvas);
            if (mImage != null) {
                mImage.recycle();
            }
        }
    }

    public void drawRect(Paint paint, HashMap<Rect, String> rectHashMap) {
        SurfaceHolder holder = getHolder();
        Canvas canvas = null;
        while (canvas == null) {
            canvas = holder.lockCanvas();
        }
        Paint mPaint = new Paint();
        mPaint.setStrokeWidth(2);
        mPaint.setTextSize(30);
        mPaint.setColor(Color.WHITE);
        mPaint.setTextAlign(Paint.Align.LEFT);
        Set<Map.Entry<Rect, String>> set = rectHashMap.entrySet();
        for (Map.Entry<Rect, String> entry : set) {
            Rect rect = entry.getKey();
            String name = entry.getValue();
            if (name == null) {
                name = "不认识";
            }
            if (canvas != null) {
                canvas.drawRect(rect, paint);
                canvas.drawText(name, rect.left, rect.bottom + 40, mPaint);
            }
        }
//		canvas.drawRect(rect,paint);
        if (canvas != null) {
            holder.unlockCanvasAndPost(canvas);
        }
    }


}